package com.woniuxy.controllers;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.*;
import com.woniuxy.services.ClientService;
import com.woniuxy.services.DevelopmentplanService;
import com.woniuxy.services.SaleService;
import com.woniuxy.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "客户开发计划管理接口")
public class DevelopmentplanController {
    @Autowired
    private DevelopmentplanService developmentplanService;
    @Autowired
    private SaleService saleService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClientService clientService;
    @GetMapping("/devps")
    @ApiOperation(
            value = "查询所有客户开发计划",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="pagenow",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pagesize",value="每页显示的条目数",required=true),
            @ApiImplicitParam(paramType="query",name="role_id",value="登录人角色id",required=true),
            @ApiImplicitParam(paramType="query",name="user_code",value="登录人用户编号",required=true),

    })
    public RestPOJO getdevp(String key,Integer pagenow,Integer pagesize,String startDate,String overDate,String role_id,String user_code){

        System.out.println("roleid:"+role_id);
        System.out.println("user_code:"+user_code);
        System.out.println("key"+key);
        System.out.println("startDate"+startDate);
        System.out.println("overDate"+overDate);
        if(key!=null&&key.equals("待执行")){
            key="0";
        }
        if(key!=null&&key.equals("已执行")){
            key="1";
        }
        System.out.println("statdate:"+startDate);
        System.out.println("ovaerdate:"+overDate);
        RestPOJO restPOJO=new RestPOJO();
        PageInfo<Developmentplan>  pageinfo=new PageInfo<Developmentplan>();
        //主管登录查询所有人的
        if(role_id.equals("3")){
            pageinfo=developmentplanService.selalldevp(key,pagenow,pagesize,startDate,overDate);
        }
        //经理登录只查询到指派人是自己的
        if(role_id.equals("4")){
            pageinfo=developmentplanService.selalldevp1(key,pagenow,pagesize,startDate,overDate,user_code);
        }
        if(pageinfo!=null&&pageinfo.getSize()>=0){
            restPOJO.setData(pageinfo);
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @GetMapping("/devps/{sale_id}")
    @ApiOperation(
            value = "查询指定销售机会ID客户开发计划",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    public RestPOJO getdevpbysaleid(@PathVariable("sale_id")String sale_id){

        RestPOJO restPOJO=new RestPOJO();
        Map<String,Object> map=new HashMap<>();
        PageInfo<Developmentplan>  pageinfo=developmentplanService.selalldevpbysaleid(sale_id);
        Sale sale=saleService.selsalebyid(sale_id);

        if(sale.getCli_id()!=null){
            Client client=clientService.selectByPrimaryKey(sale.getCli_id());
            map.put("cliname",client.getCli_name());
        }else {
            map.put("cliname","暂无");
        }
        Users usercreat=userService.selusernamebyusercode(sale.getSale_creatcode());
        Users userdue=userService.selusernamebyusercode(sale.getSale_dealcode());
        if(userdue==null){
            map.put("dealname","异常数据");
        }else{
            map.put("dealname",userdue.getUser_name());
        }
        map.put("devpdata",pageinfo);
        map.put("saledata",sale);
        map.put("creatname",usercreat.getUser_name());
        if(pageinfo!=null&&pageinfo.getSize()>=0){
            restPOJO.setData(map);
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @PostMapping("/devps/{dp_id}")
    @ApiOperation(
            value = "执行开发计划",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    public RestPOJO changedpstatus(@PathVariable("dp_id")String dp_id){
        RestPOJO restPOJO=new RestPOJO();
        int count=developmentplanService.changestatus(dp_id);
        if(count!=0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @PostMapping("/devps")
    @ApiOperation(
            value = "更改客户开发计划",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    public RestPOJO changedevp(@RequestBody Developmentplan developmentplan){
        RestPOJO restPOJO=new RestPOJO();
        int count=developmentplanService.updatedevp(developmentplan);
        if(count!=0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @PutMapping("/devps")
    @ApiOperation(
            value = "新增客户开发计划",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    public RestPOJO adddevp(@RequestBody Developmentplan developmentplan){
        RestPOJO restPOJO=new RestPOJO();
        System.out.println(developmentplan);
        int count=developmentplanService.adddevp(developmentplan);
        if(count!=0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @PutMapping("/devps/{id}/{statu}")
    @ApiOperation(
            value = "更改客户开发结果",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({

            @ApiImplicitParam(paramType="path",name="statu",value="开发结果代码，2成功，3失败",required=true),
    })
    public RestPOJO updatedevpdtatu(@PathVariable("id")String sale_id,@PathVariable("statu")String status){
        RestPOJO restPOJO=new RestPOJO();
        saleService.updatestate(sale_id,status);//修改销售机会表的开发状态
        if(status.equals("2")){
            status="开发成功";
        }
        if(status.equals("3")){
            status="开发失败";
        }
        int count=developmentplanService.updatedevpdtatu(sale_id,status);

        if(count!=0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @PostMapping("/devps/del")
    @ApiOperation(
            value = "批量删除客户开发计划",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    public RestPOJO deletdevps(@RequestBody String[] ids){
        RestPOJO restPOJO=new RestPOJO();
        int count=developmentplanService.delalldevps(ids);

        if(count!=0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }

        return restPOJO;
    }


    @PostMapping("/sucesssale")
    @ApiOperation(
            value = "开发成功新建客户，联系人成功后给对应的销售机会插入联系人id",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "{\"cliid\":\"data\",\"saleid\":\"data\"}"
    )

    public RestPOJO succsessale(@RequestBody Map<String,Object> map){
        String saleid=map.get("saleid").toString();
        String cliid=map.get("cliid").toString();
        System.out.println("saleid:   "+saleid);
        System.out.println("cliid:    "+cliid);
        RestPOJO restPOJO=new RestPOJO();
        int count=saleService.updsale_cliid(saleid,cliid);
        if(count!=0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
}
